package com.rfid.project.web;

import java.text.DateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.data.domain.Page;

import biz.appframe.PageJsonBean;

import com.rfid.project.model.ProjectDeliveryDetail;
import com.rfid.project.service.ProjectDeliveryDetailService;
import com.rfid.project.util.StringUtil;

@Controller
public class ProjectDeliveryDetailController {

	private static final Logger logger = LoggerFactory
			.getLogger(ProjectDeliveryDetailController.class);

	@Autowired
	private ProjectDeliveryDetailService projectDeliveryDetailService;

	@RequestMapping(value = "/projectDeliveryDetailmgr")
	public String toMenumgr(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		return "projectDeliveryDetailmgr";
	}

	@RequestMapping(value = "/queryProjectDeliveryDetail")
	public String queryProjectDeliveryDetail(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "projectDeliveryId", defaultValue = "-1") Long projectDeliveryId,
			@RequestParam(value = "productSequenceNo", defaultValue = "") String productSequenceNo,
			@RequestParam(value = "eTag", defaultValue = "") String eTag,
			@RequestParam(value = "remark", defaultValue = "") String remark,
			@RequestParam(value = "page", defaultValue = "1") Integer pageNumber,
			@RequestParam(value = "rows", defaultValue = "20") Integer pageSize) {
		Page<ProjectDeliveryDetail> projectDeliveryDetails = projectDeliveryDetailService
				.findAll(projectDeliveryId, productSequenceNo, eTag, remark, pageNumber,
						pageSize, null, null);

		JSONObject object = new JSONObject();
		object.put("total", projectDeliveryDetails.getTotalElements());

		JSONArray models = new JSONArray();
		for (ProjectDeliveryDetail projectDeliveryDetail : projectDeliveryDetails) {
			JSONObject mod = new JSONObject();
			wrapData(projectDeliveryDetail, mod);

			models.add(mod);
		}
		object.put("rows", models);

		PageJsonBean.printObject(response, object);
		return null;
	}
	@RequestMapping(value = "/findProjectDeliveryDetail")
	public String findProjectDeliveryDetail(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "id", defaultValue = "-1") Long id){
		ProjectDeliveryDetail projectDeliveryDetail = projectDeliveryDetailService.findOne(id);
		
		JSONObject object = new JSONObject();
		JSONObject mod = new JSONObject();
		wrapData(projectDeliveryDetail, mod);
		object.put("success", true);
		object.put("data", mod);
		PageJsonBean.printObject(response, object);
		return null;
	}

	private void wrapData(ProjectDeliveryDetail projectDeliveryDetail,
			JSONObject mod) {
		mod.put("id", projectDeliveryDetail.getId());
		mod.put("productSequenceNo", projectDeliveryDetail.getProductSequenceNo());
		mod.put("projectDeliveryId", projectDeliveryDetail.getProjectDelivery().getId());
		mod.put("eTag", projectDeliveryDetail.getETag());
		mod.put("remark", StringUtil.getString(projectDeliveryDetail.getRemark()));
	}

	@RequestMapping(value = "/queryProjectDeliveryDetailByProjectDeliveryId")
	public String queryProjectDeliveryDetailByProjectDeliveryId(
			HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "projectDeliveryId", defaultValue = "-1") Long projectDeliveryId,
			@RequestParam(value = "remark", defaultValue = "") String remark,
			@RequestParam(value = "page", defaultValue = "1") Integer pageNumber,
			@RequestParam(value = "rows", defaultValue = "20") Integer pageSize) {

		Page<ProjectDeliveryDetail> projectDeliveryDetails = projectDeliveryDetailService
				.findByProjectDeliveryId(projectDeliveryId, pageNumber,
						pageSize, null, null);

		JSONObject object = new JSONObject();
		object.put("total", projectDeliveryDetails.getTotalElements());

		JSONArray models = new JSONArray();
		for (ProjectDeliveryDetail projectDeliveryDetail : projectDeliveryDetails) {
			JSONObject mod = new JSONObject();
			mod.put("id", projectDeliveryDetail.getId());
			mod.put("productSequenceNo",
					projectDeliveryDetail.getProductSequenceNo());
			mod.put("eTag", projectDeliveryDetail.getETag());
			String remark_ = projectDeliveryDetail.getRemark();
			mod.put("remark", remark_ == null ? "" : remark_);

			models.add(mod);
		}
		object.put("rows", models);

		PageJsonBean.printObject(response, object);
		return null;
	}

	@RequestMapping(value = "/editProjectDeliveryDetail")
	public String editProjectDeliveryDetail(HttpServletRequest request,
			HttpServletResponse response,
			@Valid ProjectDeliveryDetail projectDeliveryDetail, Model model,
			RedirectAttributes redirectAttributes) {
		this.projectDeliveryDetailService
				.saveProjectDeliveryDetail(projectDeliveryDetail);

		JSONObject result = new JSONObject();
		result.put("success", true);
		PageJsonBean.printObject(response, result);
		return null;
	}

	@RequestMapping(value = "/delProjectDeliveryDetail")
	public String delProjectDeliveryDetail(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam(value = "id", defaultValue = "") Long id) {
		JSONObject result = new JSONObject();
		boolean flag = true;
		this.projectDeliveryDetailService.deleteProjectDeliveryDetail(id);
		result.put("success", flag);
		PageJsonBean.printObject(response, result);
		return null;
	}

	@InitBinder
	protected void initBinder(HttpServletRequest request,
			ServletRequestDataBinder binder) throws Exception {
		DateFormat dateFormat = com.rfid.project.util.DateUtil.getFormat();
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(
				dateFormat, true));
	}

}
